package com.zllkj.mm.module.freetime

import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.zllkj.mm.R
import com.zllkj.mm.adapter.FragmentBaseAdapter
import com.zllkj.mm.base.BaseFragment
import com.zllkj.mm.base.BaseTitleFragment
import kotlinx.android.synthetic.main.activity_order_list.view.tb_order_list_tab
import kotlinx.android.synthetic.main.fragment_my_freetime.view.*

class MyFreeTimeFragment:BaseTitleFragment() {

    private val titles = arrayOf("待确认闲时", "核销", "核销记录")
    private val freetimeFragmentList = arrayListOf<BaseFragment>()

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_my_freetime
    }

    override fun loadData() {
        super.loadData()
        setCommonTitle("我的闲时")
        freetimeFragmentList.add(WaitToConfirmFreeTimeOrderFragment())
        freetimeFragmentList.add(toDoVerificationFragment())
        freetimeFragmentList.add(OrderVerificationHistoryFragment())


        mRootView?.findViewById<TabLayout>(R.id.tb_order_list_tab)?.let { tablayout ->
            titles.mapIndexed { index, s -> mRootView?.tb_order_list_tab?.addTab(tablayout.newTab().setText(s))}
        }

        mRootView?.vp_my_freetime_list?.adapter= FragmentBaseAdapter(childFragmentManager, freetimeFragmentList, titles)
        mRootView?.tb_order_list_tab?.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                mRootView?.vp_my_freetime_list?.setCurrentItem(tab.position)
            }
            override fun onTabUnselected(tab: TabLayout.Tab) {}
            override fun onTabReselected(tab: TabLayout.Tab) {}
        })

        mRootView?.vp_my_freetime_list?.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener(){

            override fun onPageSelected(p0: Int) {
                mRootView?.tb_order_list_tab?.let {
                    it.setScrollPosition(p0,0f,false)
                }
            }
        })
    }
}